<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
	/*
	 ×后台登录首页
	 */

	public function login(){
        if(!empty($_SESSION['adminid'])){
        	$this->redirect('Index/index');
        }else{
        	 $this->display('index');
        }
       

	}
	//验证用户名和密码
	public function verifyUser()
    {
    	// 检测验证码
		    $code=I('post.vercode','','strip_tags');

		    $data['uname']=I('post.username','','strip_tags');
		    $data['password']=md5(I('post.password','','strip_tags'));
		    $data['_logic']='AND';
		    $verify = new \Think\Verify();
		    $result['vercode']=$verify->check($code);		    
		    if(empty($result['vercode'])){
               echo "<script type='text/javascript'>alert('验证码错误请重新填写'); window.history.go(-1);window.location.reload();</script>";
               return '';
		    }

		    //验证用户名是否存在
		    $result['password']= M('admin')->where("uname='%s'",$data['uname'])->getField('password');
		    if(empty($result['password'])){
                echo "<script type='text/javascript'>alert('用户名不存在'); window.history.go(-1);window.location.reload();</script>";
		    }else{
               $result['adminid']=M('admin')->where($data)->getField('adminid');
               if(empty($result['adminid'])){
                    
                    echo "<script type='text/javascript'>alert('密码错误'); window.history.go(-1);window.location.reload();</script>";
               }else{
               	 $_SESSION['uname']=$data['uname'];
               	 $_SESSION['adminid']=$result['adminid'];
               	// R('Adminlog/addlog',  array(array('登录','后台')));
               	$this->redirect('Login/index');
               }
		    }

    }
	 public function logout()
    {

    	if(!empty($_SESSION['adminid'])){
            
			$_SESSION=array();
			$this->login();
		}else{
			$this->login();
		}
    }
    //生成验证码
	public function vercode()
	{
		$Verify = new \Think\Verify();
		$Verify->fontSize =15;
		$Verify->length   = 4;
		$Verify->useNoise = false;
        $Verify->entry();
	}

	//空操作
	public function _empty()
	{
		$this->login();
	}
}